// ==UserScript==
// @name         ABA历史数据
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.sif.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=sif.com
// @updateURL    https://gitee.com/great-pacey/tampermonkey/raw/master/ABAHistory.user.js
// @grant        GM_registerMenuCommand
// @grant        GM_xmlhttpRequest
// @grant        GM_setClipboard
// @grant        unsafeWindow
// ==/UserScript==

(function () {
    let ConsecutiveTimes = 0
    const LONG_DURATION = 1000 * 30
    const SHORT_DURATION = 1000 * 2
    let TOTAL_PAGE
    let TIMES
    let TipEl
    let NoneDateCount = 0
    let lastFlag = false
    const country = parseString(window.location.search).country

    function parseString(url){
        var urlParams = new URLSearchParams(url)
        var paramsObj = {};
        for(var key of urlParams.keys()) {
            for(var val of urlParams.values()) {
                paramsObj[key] = val
            }
        }
        return paramsObj
    }

    function setTIMES() {
        const d = new Date()
        TIMES = ['11', '12']
        TIMES = TIMES.map(i => (d.getFullYear() - 1) + '-' + i)
        TIMES.unshift(d.getFullYear() + '-' + formatNum(d.getMonth()))
    }

    function formatNum(n) {
        return n <= 9 ? '0' + n : n;
    }

    function setTip(num) {
        if (!TipEl) {
            TipEl = document.createElement('div')
            TipEl.style.marginLeft = '15px'
            document.querySelector('div.search-con').appendChild(TipEl)
        }
        TipEl.innerText = `抓取中(${num}/${TOTAL_PAGE})`
    }

    function removeTip() {
        TipEl.parentElement.removeChild(TipEl)
    }

    async function preWait() {
        if (shouldWait()) {
            await wait(LONG_DURATION)
            ConsecutiveTimes = 0
        } else {
            await wait(SHORT_DURATION)
        }
    }

    async function getGroupIds() {
        return new Promise(resolve => {
            const authorization = unsafeWindow.localStorage.token
            GM_xmlhttpRequest({
                method: "GET",
                url: `https://www.sif.com/api/user/groupKeywrod/scan?country=${country}&sortBy=&`+Date.now(),
                headers: {
                    authorization,
                    Accept: 'application/json, text/plain, */*',
                    Origin: `https://www.sif.com`,
                    'Referer': `https://www.sif.com/words?country=${country}`,
                    'Content-Type': 'application/json;charset=UTF-8',
                    'Sec-Fetch-Site': `same-origin`
                },
                onload(p) {
                    const token = p.responseHeaders.match(/authorization\:\s(.+)\r\nconnection/)[1]
                    unsafeWindow.localStorage.token = token
                    ConsecutiveTimes++
                    resolve(JSON.parse(p.responseText).data)
                }
            })
        })
    }

    async function getGroupId() {
        const selectedGroupName = document.querySelector('#words .title-top .group input.el-input__inner').value.trim()
        const groupIds = await getGroupIds()
        let groupid
        try {
            groupid = groupIds.find(i => i.groupName === selectedGroupName).groupid
        } catch (error) {
            alert('请选择关键词库，重新开始')
            exit()
        }
        return groupid
    }

    async function getDataInBulk() {
        const groupId = await getGroupId()
        let pageNum = 1
        let result = []
        const data = (await getDataByGroupId(groupId, pageNum)).data
        if (data.keywords.length === 0) {
            NoneDateCount++
            lastFlag = true
        }
        if (lastFlag && NoneDateCount >=3) {
            alert('获取数据太频繁，请换个时间再获取')
            return false;
        }
        result = result.concat(data.keywords)
        TOTAL_PAGE = Math.ceil(data.total / 100)
        console.log(TOTAL_PAGE)
        while(hasNextPage(pageNum)) {
            setTip(pageNum)
            result = result.concat((await getDataByGroupId(groupId, ++pageNum)).data.keywords)
        }
        return result
    }

    function hasNextPage(pageNum) {
        if (!TOTAL_PAGE) return true
        return TOTAL_PAGE > pageNum
    }

    async function getDataByGroupId(groupId, pageNum) {
        await preWait()
        let txt = await getData(groupId, pageNum)
        while (txt.includes('访问过于频繁，请稍后重试')) {
            await wait(LONG_DURATION)
            ConsecutiveTimes = 0
            txt = await getData(groupId, pageNum)
        }
        return JSON.parse(txt)
    }

    async function getData(groupId, pageNum = 1) {
        return new Promise(resolve => {
            const data = {
                desc: true,
                groupid: groupId,
                pageNum,
                pageSize: 100,
                searchKeyword: '',
                sortBy: 'searchesRank'
            }

            const authorization = unsafeWindow.localStorage.token

            GM_xmlhttpRequest({
                method: "POST",
                url: `https://www.sif.com/api/search/focusKeywords?country=${country}&_t=`+Date.now(),
                headers: {
                    authorization,
                    Accept: 'application/json, text/plain, */*',
                    Origin: `https://www.sif.com`,
                    'Referer': `https://www.sif.com/words?country=${country}`,
                    'Content-Type': 'application/json;charset=UTF-8',
                    'Sec-Fetch-Site': `same-origin`
                },
                data: JSON.stringify(data),
                onload: function (p) {
                    const token = p.responseHeaders.match(/authorization\:\s(.+)\r\nconnection/)[1]
                    unsafeWindow.localStorage.token = token
                    ConsecutiveTimes++
                    resolve(p.responseText)
                }
            })
        })
    }

    async function wait(num = 1000) {
        return new Promise(resolve => {
            setTimeout(resolve, num)
        })
    }

    function shouldWait() {
        return ConsecutiveTimes >= 13
    }

    async function main() {
        setTIMES()
        const data = (await getDataInBulk())
        if (!data) return
        console.log(data)
        removeTip()
        let r = data.map(i => i.keyword + '\t' + c(i.estSearchesNumHistory))
        const header = '关键词\t' + TIMES.map(formatDate).join('\t')
        alert('复制成功，直接在EXCEL粘贴')
        GM_setClipboard(header + '\n' + r.join('\n'))
    }

    function formatDate(d) {
        return d.split('-')[0] + '年' + d.split('-')[1] + '月'
    }

    GM_registerMenuCommand('抓取', main)

    function c(o) {
        let searchesRank = o.searchesRank
        let date = o.date
        let result = []
        TIMES.forEach(i => {
            let index = date.indexOf(i)
            if (index === -1) {
                result.push('')
            } else {
                result.push(searchesRank[index])
            }
        })

        return result.join('\t')
    }
})()
